package com.ws.shop.util;

import java.text.NumberFormat;
import java.util.Date;

public class StringUtil {
	public static int outFileCounter = 0;
	// 输出orderflow文件序号的最大值
	public static int outFileCounterE = 1000;

	public static int processid = 1;// 进程号id

	public static boolean isEmpty(String str) {
		return null == str || "".equals(str.trim())
				|| "null".equalsIgnoreCase(str.trim());
	}

	static char[] takeArr = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
			'A', 'B', 'C', 'D', 'E', 'F',
			'G', 'H', 'I', 'J', 'K', '0', '1', '2', '3', '4', '5', '6', '7',
			'8', '9', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
			'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8',
			'9' };

	static char[] numArr = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
	static char[] charArr = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
		'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V','W', 'X', 'Y', 'Z'};
	
	public static String getNumRandomStr(int length){
		char[] result = new char[length];
		int len = numArr.length;
		synchronized (numArr) {
			for (int i = 0, j = len; i < length; ++i, --j) {
				int take = (int) (Math.random() * j);
				result[i] = numArr[take];
				char m = numArr[j - 1];
				numArr[j - 1] = numArr[take];
				numArr[take] = m;
			}
		}
		return new String(result);
	}
	
	public static String getCharRandomStr(int length){
		char[] result = new char[length];
		int len = charArr.length;
		synchronized (charArr) {
			for (int i = 0, j = len; i < length; ++i, --j) {
				int take = (int) (Math.random() * j);
				result[i] = charArr[take];
				char m = charArr[j - 1];
				charArr[j - 1] = charArr[take];
				charArr[take] = m;
			}
		}
		return new String(result);
	}
	
	/**
	 * 获取一个随机字符
	 * 
	 * @return
	 */
	public static char getRandomS() {
		synchronized (takeArr) {
			return takeArr[(int) (Math.random() * takeArr.length)];
		}
	}

	/**
	 * 获取一个随机字符串
	 * 
	 * @return 随机字符串
	 */
	public static String getRandomStr(int length) {
		char[] result = new char[length];
		int len = takeArr.length;
		synchronized (takeArr) {
			for (int i = 0, j = len; i < length; ++i, --j) {
				int take = (int) (Math.random() * j);
				result[i] = takeArr[take];
				char m = takeArr[j - 1];
				takeArr[j - 1] = takeArr[take];
				takeArr[take] = m;
			}
		}
		return new String(result);
	}

	/**
	 * 获取一个随机字符串
	 * 
	 * @return 随机字符串
	 */
	public static String getRandomStr8() {
		char[] result = new char[8];
		int len = takeArr.length;
		synchronized (takeArr) {
			for (int i = 0, j = len; i < 8; ++i, --j) {
				int take = (int) (Math.random() * j);
				result[i] = takeArr[take];
				char m = takeArr[j - 1];
				takeArr[j - 1] = takeArr[take];
				takeArr[take] = m;
			}
		}
		return new String(result);
	}

	public static String getRnadomStr6() {
		char[] result = new char[6];
		int len = takeArr.length;
		synchronized (takeArr) {
			for (int i = 0, j = len; i < 6; ++i, --j) {
				int take = (int) (Math.random() * j);
				result[i] = takeArr[take];
				char m = takeArr[j - 1];
				takeArr[j - 1] = takeArr[take];
				takeArr[take] = m;
			}
		}
		return new String(result);
	}

	public synchronized static String getSeq() {
		if (outFileCounter < outFileCounterE) {
			outFileCounter++;
		} else {
			outFileCounter = 0;
		}
		// 数字格式化
		NumberFormat nf = NumberFormat.getInstance();
		nf.setMinimumIntegerDigits(5);
		nf.setGroupingUsed(false);
		return nf.format(outFileCounter);
	}

	public synchronized static String getID() {
		if (outFileCounter < 999) {
			outFileCounter++;
		} else {
			outFileCounter = 0;
		}
		// 数字格式化
		NumberFormat nf = NumberFormat.getInstance();
		nf.setMinimumIntegerDigits(3);
		nf.setGroupingUsed(false);
		return processid + DateUtil.dateToString(new Date(), DateUtil.YYMMDDHHMMSSSSS)
				+ nf.format(outFileCounter);
	}

	// 取指定字符串后面index位
	public static String getallocatstr(String s, int index) {
		if (s == null) {return "";}
		int tlen = s.length();
		int slen = tlen - index;
		if (slen < 0) {
			slen = 0;
		}
		String destr = s.substring(slen);
		return destr;
	}

	//生成6位数字
	
	public static String phonedigst(){
		String s = DateUtil.dateToString(new Date(), DateUtil.YYMMDDHHMMSSSSS);
		return getallocatstr(s, 6);
	}
	
	public static int[] string2IntArray(String src,String separate){
		if(StringUtil.isEmpty(src))
			return null;
		String arr[] = src.split(separate);
		int[] tarr = new int[arr.length];
		for(int i = 0 ; i < arr.length ; i++){
			tarr[i] = Integer.parseInt(arr[i]);
		}
		return tarr;
	}
	
	public static String intArray2String(int[] imgs){
		StringBuilder sb = new StringBuilder("[");
		if(imgs != null && imgs.length > 0){
			for(Object t : imgs){
				sb.append(t).append(";");
			}
		}
		sb.append("]");
		return sb.toString();
	}
	
	public static String intArray2String(int[] arr,String separate){
		StringBuilder sb = new StringBuilder();
		for(int t : arr){
			sb.append(t).append(separate);
		}
		if(sb.length() > 0)
			sb.deleteCharAt(sb.length()-1);
		return sb.toString();
	}
}
